package bidsanity



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(BidController)
@Mock([Customer, Bid, Listing])
class BidControllerTests {
    
    void testCreate(){
        def cust = controller.create()
        Assert.assertNotNull(cust)
    }

    void testSuccessfulSave() {
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save()
        def b = new Customer(emailAddress: 'bidder@example.org', password: "testing").save()
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save()
        Map myParams = ['bidder': b, 'listing': l, amount: '2.0']  //this 2.0 needs to be a string
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertTrue(flash.message == 'bid.successful')
        Assert.assertTrue(response.redirectedUrl == "/listing/show/${l.id}")
    }

    void testFailedBidAmount() {
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save()
        def b = new Customer(emailAddress: 'bidder@example.org', password: "testing").save()
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save()
        Map myParams = ['bidder': b, 'listing': l]  //for some reason this 2.0 needs to be a string
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertTrue(flash.message == 'bid.was.not.valid')
        Assert.assertTrue(response.redirectedUrl == "/listing/show/${l.id}")
    }

//    Moved to integration tests
//    void testFailedBidTooLow() {
//        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
//        def b = new Customer(emailAddress: 'bidder@example.org', password: "testing").save(validate: true)
//        def l = new Listing(
//                name: 'TestListing',
//                endDate: new Date() + 10,
//                startingBidPrice: 1.0D,
//                description: 'a test listing',
//                seller: s)
//        l.addToBids(new Bid(bidder: b, listing:  l, amount: 12.0D))
//        l.save(validate: true)
//        Assert.assertFalse l.hasErrors()
//        Map myParams = ['bidder': b, 'listing': l, amount: '2.0']
//        controller.params.putAll(myParams)
//        controller.save()
//        println(flash.message)
//        println(response.redirectedUrl)
////        Assert.assertTrue(flash.message == 'bid.bid.too.low')
////        Assert.assertTrue(response.redirectedUrl == "/listing/show/${l.id}")
//        Assert.fail('not working...')
//    }

}
